/**
 * Created by asus on 2017/4/19.
 */
$(function(){
    //直播列表动效
    var listMove = function(){
        var listPic = $(".listPic");
        var liveListBox = $(".livingBox .listBox");
        var backListBox = $(".backBox .listBox");
        $.each(liveListBox,function(index){
            if((index+1)%3 == 0){
                $(this).addClass("tripleBox");
            }
        });
        $.each(backListBox,function(index){
            if((index+1)%3 == 0){
                $(this).addClass("tripleBox");
            }
        });
        listPic.hover(function(){
            $(this).find(".play_layer").stop().show();
            //$(this).find("img").animate({width:"340px"});
            $(this).find("img").addClass("big")
        },function(){
            $(this).find(".play_layer").stop().hide();
            //$(this).find("img").animate({width:"340px"});
            $(this).find("img").removeClass("big")

        });
    };

    var Live_Wrapper = $(".Live_Wrapper");
    //Live_Wrapper.hide();
    layui.use('layer', function(){
        var layer = layui.layer;
        layer.ready(function(){
            //var load_index = layer.load(0,{shade: [0.2, '#333']});

            var layer_pay_index;
            //付费观看直播
            var viewLive = function(that){
                var live_url = $(that).data("live_url"),
                    l_channel = $(that).data("l_channel"),
                    s_price = $(that).data("s_price")*100/100;

                var pay_info_html =
                    '<div class="pay_info_wra">'+
                    '<span class="close_btn fr"></span>'+
                    '<h4 class="LB_num">'+s_price+'</h4>'+
                    '<p class="LB_tip">观看直播将消耗你'+s_price+'L币</p>'+
                    '<p class="balance_acc">账户余额：'+localStorage.u_balance+'L币</p>'+
                    '<a class="view_btn" href="javascript:void(0)">确认观看</a>'+
                    '</div>';
                //<!--<p class="balance_acc_no">余额不足</p>-->
                //<!--<a class="view_btn" href="##">L币充值</a>-->
                //layer.ready(function(){
                layui.use('layer', function(){
                    var layer = layui.layer;
                    layer_pay_index = layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 0,
                        shadeClose: false, //开启遮罩关闭
                        area: ['434px', '324px'],
                        skin: 'layui-layer-molv',
                        content: pay_info_html,
                        resize: true,
                        //fixed:true,
                        //scrollbar:false,
                        offset: '150px',
                        id: "212"
                    });
                    if(s_price>localStorage.u_balance){
                        $(".view_btn").html("去充值");
                        window.location.href = "../recharge/index.html"
                    }else {
                        $(".view_btn").html("确认观看");
                        $(".view_btn").on("click",function(){
                            //  支付观看直播
                            var live_param = {
                                url:'Homes/payment',
                                data:{
                                    channel: l_channel,
                                    price: s_price
                                }
                            };
                            var newWin = window.open();
                            $.JsonRpc(live_param,function(data){
                                layer.close(layer_pay_index);
                                if(data.code==200){
                                    localStorage.u_balance = data.data.u_balance;
                                    //解决拦截弹出式窗口  将window.open()放在click事件中
                                    newWin.location = live_url;
                                }else if(data.code==400){
//                                        alert(data.data.message);
                                    layer.msg(data.data.message,{offset:'150px',time: 2*1000,shadeClose: false});
                                }
                            });
                        });
                    }

                    $(".close_btn").on("click",function(){
                        layer.close(layer_pay_index);
                    });
                });
            };
            Live_Wrapper.on("click",".livingBox .listPic",function(){
                viewLive($(this));
            });
            function addStar (){
                $(".add_star_btn").off("click").on("click",function(){
                    var btn_now = $(this);
                    if(btn_now.hasClass("ajaxDisable")){
                        console.log("操作频繁，无效点击");
                        return false;
                    }else {
                        btn_now.addClass("ajaxDisable");
                    }
                    var shout_param = {
                        url:'Gifts/bright',
                        data:{
                            shout:$(this).data("s_id")
                        }
                    };
                    //=================================================================================================
                    var exchange_content =
                        '<div id="exchange_wrap">'+
                        '<div class="title">亲，你的 <span class="tips">50次</span>免费加星机会已用光 <span class="close_btn fr"></span></div>'+
                        '<p class="cite">L币兑换加星机会</p>'+
                        '<ul class="call_gift_wrap clearfix">'+
                        '</ul>'+
                        '<span class="exchange_btn">立即兑换</span>'+
                        '<div class="bot">'+
                        '<p class="balance_wrap">账户余额： <span>'+localStorage.u_balance+'</span>L币</p><a href="'+url_r+'" class="recharge_btn">充值</a>'+
                        '</div>'+
                        '</div>';

                    $.JsonRpc(shout_param,function(data){
                        btn_now.removeClass("ajaxDisable");
                        if(data.code==400){
                            //alert(data.data.message);
                            //呼唤明星兑换礼物
                            //Gifts/giftList
                            layui.use('layer', function(){
                                var layer = layui.layer;
                                layer.ready(function(){
                                    var index = layer.open({
                                        id:"layer_exchange",
                                        type: 1,
                                        title: false,
                                        closeBtn: 0,
                                        shadeClose: false, //开启遮罩关闭
                                        area: ['560px', ''],
                                        skin: 'layui-layer-molv',
                                        content: exchange_content,
                                        resize:false,
                                        offset:'150px'
                                    });
                                    //  layer.close(index);
                                    //  console.log(index);
                                    $("#exchange_wrap .close_btn").on("click",function(){
                                        layer.close(index);
                                    });
                                    var call_gift_wrap = $(".call_gift_wrap");
                                    var gift_param = {
                                        url:'Gifts/giftList',
                                        data:{
                                            type:250
                                        }
                                    };
                                    $.JsonRpc(gift_param,function(result){
                                        result = result.data.list;
                                        var tpl="";
                                        $.each(result,function(k){
                                            var li_text = result[k].c_name.split("LB");
                                            tpl +=
                                                '<li data-cid="'+result[k].cid+'" data-value="'+result[k].c_sort+'" class="call_gift_item fl">'+
                                                '<span class="call_time">'+li_text[1]+'</span>'+
                                                '<span class="exchange_money">'+li_text[0]+'L币</span>'+
                                                '</li>';
                                        });
                                        call_gift_wrap.html(tpl);

                                        $(".call_gift_item").on('click',function(){
                                            $(this).addClass("active").siblings("li").removeClass("active");
                                        });
                                        /*  $(".call_gift_item").on('hover',function(){
                                         $(this).addClass("active");
                                         },function(){
                                         $(this).removeClass("active");
                                         });*/
                                        $(".exchange_btn").off("click").on('click',function(){
                                            var exchange_btn_now = $(this);
                                            if(exchange_btn_now.hasClass("ajaxDisable")){
                                                return false;
                                            }
                                            exchange_btn_now.addClass("ajaxDisable");
                                            if(!$(".call_gift_wrap").children(".active")[0]){
                                                layer.alert('请先选择购买次数', {title:false,closeBtn:0,offset:'200px'}, function(index1){
                                                    layer.close(index1);
                                                    exchange_btn_now.removeClass("ajaxDisable");
                                                    return false;
                                                });
                                            }
                                            if(localStorage.u_balance<call_gift_wrap.children(".active").data("value")){
                                                layer.alert('L币不足，请点击充值', {title:false,closeBtn:0,offset:'200px'}, function(index2){
                                                    layer.close(index2);
                                                    exchange_btn_now.removeClass("ajaxDisable");
                                                    return false;
                                                });
                                            }
                                            var exchange_param = {
                                                url:'Recharges/pay',
                                                data:{
                                                    type:40,
                                                    cid:call_gift_wrap.children(".active").data("cid"),
                                                    amount:call_gift_wrap.children(".active").data("value")
                                                }
                                            };
                                            $.JsonRpc(exchange_param,function(data){
                                                console.log(data);
                                                if(data.code==200){
                                                    //layer.close(index);
                                                    layer.closeAll();
                                                    layer.alert(data.data.message,{title:false,closeBtn:0,offset:'150px'}, function(index3){
                                                        var u_balance_new = iService.accSub(localStorage.u_balance,call_gift_wrap.children(".active").data("value"));
                                                        localStorage.u_balance = u_balance_new;
                                                        $(".balance_wrap span").html(u_balance_new);
                                                        exchange_btn_now.removeClass("ajaxDisable");
                                                        layer.close(index3);
                                                        return false;
                                                    });
                                                }
                                            });
                                        });
                                    });
                                });
                            });
                        }else if(data.code==200){
                            var s_number_wrap = btn_now.parents(".call_item").find(".s_number");
                            var add_num = btn_now.parents(".call_item").find(".add_num");
                            data = data.data;
                            var time_total;
                            if(data.status == 10){
                                time_total = data.number;
                                s_number_wrap.html(time_total);
                                add_num.html("+&nbsp;"+data.num);
                                layui.use('layer', function(){
                                    var layer = layui.layer;
                                    layer.ready(function(){
                                        layer.msg('加星成功',{icon: 1,offset:'150px',time: 2*1000,shadeClose: false});
                                    })
                                });
                            }else if(data.status == 20){
                                /* time_rest = data.number; //剩余次数
                                 alert(data.message+"剩余次数："+time_rest);
                                 s_number_wrap.html(parseInt(s_number_wrap.html())-1);*/
                            }
                        }
                    });
                });
            }

            var living_box = $(".livingBox"),
                call_box = $(".callBox"),
                back_box = $(".backBox"),
                live_more = $(".live_more"),
                call_more = $(".call_more"),
                back_more = $(".back_more"),
                load_mark = $(".layui-flow-more"),
                live_null_html = '<div class="null-tips"><a href="javascript:;"><i>当前没有明星直播！</i></a></div>',
                call_null_html = '<div class="null-tips"><a href="javascript:;"><i>当前没有明星！</i></a></div>',
                back_null_html = '<div class="null-tips"><a href="javascript:;"><i>当前没有回放！</i></a></div>';

            var getHomeData = function(){
                var param = {
                    url:'Homes/homeList',
                    data:{
                        page:'1',
                        live:'9',
                        advance:'0',
                        play:'9',
                        shout:'6'
                    }
                };

                $.JsonRpc(param,function(data) {
                    console.log(data);
                    var live_data = data.data.live,
                        shout_data = data.data.shout,
                        playback_data = data.data.play,
                        advance_data = data.data.advance;
                        //live_data.num =0;
                    if(!live_data.num){
                        live_more.html("已经加载全部").addClass("disabled");
                        living_box.html(live_null_html);
                    }else {
                        var tpl_live = "";
                        var live_num= live_data.num;
                        var live_list = live_data.list;
                        var liveNowPage = live_data.page;
                        if(Math.ceil(live_num/live_list.length)==liveNowPage){
                            live_more.html("已经加载全部").addClass("disabled");
                        }else {
                            live_more.html("加载更多");
                        }
                        $.each(live_list,function(i){
                            tpl_live += pushLiveHtml(live_list,i,liveNowPage);
                        });
                        load_mark.eq(0).before(tpl_live);
                        listMove();
                        load_mark.eq(0).hide();
                    }
                    if(!shout_data.num){
                        call_more.html("已经加载全部").addClass("disabled");
                        call_box.html(call_null_html);
                    }else {
                        var tpl_call = "";
                        var call_num= shout_data.num;
                        var call_list = shout_data.list;
                        var callNowPage = shout_data.page;
                        if(Math.ceil(call_num/call_list.length)==callNowPage){
                            call_more.html("已经加载全部").addClass("disabled");
                        }else {
                            call_more.html("加载更多");
                        }
                        $.each(call_list,function(i){
                            tpl_call += pushCallHtml(call_list,i,callNowPage);
                        });
                        load_mark.eq(1).before(tpl_call);
                        addStar();
                        load_mark.eq(1).hide();
                    }
                    if(!playback_data.num){
                        back_more.html("已经加载全部").addClass("disabled");
                        back_box.html(back_null_html);
                    }else {
                        var tpl_back = "";
                        var back_num= playback_data.num;
                        var back_list = playback_data.list;
                        var backNowPage = playback_data.page;
                        if(Math.ceil(back_num/back_list.length)==backNowPage){
                            back_more.html("已经加载全部").addClass("disabled");
                        }else {
                            back_more.html("加载更多");
                        }
                        $.each(back_list,function(i){
                            tpl_back += pushBackHtml(back_list,i,backNowPage);
                        });
                        load_mark.eq(2).before(tpl_back);
                        listMove();
                        load_mark.eq(2).hide();
                    }
                });
            };


            function pushLiveHtml(list_data,i){
                var liveTime = String(list_data[i].l_createtime).split(":");
                var img_src = list_data[i].s_image||list_data[i].l_image;
                var live_html =
                    '<div class="listBox">'+
                    '<div class="listPic"  data-l_channel = "'+list_data[i].l_channel+'" data-s_price="'+list_data[i].s_price+'"  data-live_url="../Live/living?roomid='+list_data[i].l_channel+'&suid='+list_data[i].l_udid+'">'+
                    '<a href="javascript:void(0)">'+
                    '<img src="'+img_src+'" alt="">'+
                    '<p class="time">已开播'+liveTime[0]+'小时'+liveTime[1]+'分</p>'+
                    '<span class="play_layer"><i></i></span>'+
                    '</a>'+
                    '</div>'+
                    '<div class="list_user_info clearfix">'+
                    '<span class="fl list_user_name">'+list_data[i].s_name+'</span>'+
                    '<p class="fr num">'+
                    '<span>'+list_data[i].l_click+'</span>'+
                    '<i></i>'+
                    '</p>'+
                    '</div>'+
                    '</div>';
                return live_html;
            }
            function pushCallHtml(list_data,i,nowPage){
                var img_src = list_data[i].s_image||list_data[i].l_image;
                var call_html =
                    '<div class="call_item clearfix">'+
                    '<i class="rank_num fl">'+((nowPage-1)*6+i+1)+'</i>'+
                    '<img class="fl" src="'+img_src+'" alt="">'+
                    '<div class="info fl">'+
                    '<h3>'+list_data[i].s_name+'</h3>'+
                    '<p>'+
                    '<i class="ic_l_star"></i><span class="s_number">'+list_data[i].s_number+'</span>'+
                    '<span><span class="num s_comment">'+list_data[i].s_comment+'</span>留言</span>'+
                    '</p>'+
                    '</div>'+
                    '<div class="fr add_star_wrap">'+
                    '<span data-s_id = "'+list_data[i].sid+'" class="add_star_btn">加星</span>'+
                    '<p>已为Ta<span class="add_num">+&nbsp;<b>'+list_data[i].num+'</b></span><i class="ic_g_star"></i></p>'+
                    '</div>'+
                    '</div>';
                return call_html;
            }
            function pushBackHtml(list_data,i){
                var img_src = list_data[i].s_image||list_data[i].l_image;
                var back_html =
                    '<div class="listBox">'+
                    '<div class="listPic" data-l_channel = "'+list_data[i].l_channel+'" data-s_price="'+list_data[i].s_price+'"  data-back_url="../playback/index?roomid='+list_data[i].l_channel+'&suid='+list_data[i].l_udid+'">'+
                    '<a href="../playback/index?roomid='+list_data[i].l_channel+'&suid='+list_data[i].l_udid+'">'+
                    '<img src="'+img_src+'" alt="">'+
                    '<p class="time">回放</p>'+
                    '<span class="play_layer"><i></i></span>'+
                    '</a>'+
                    '</div>'+
                    '<div class="list_user_info clearfix">'+
                    '<span class="fl list_user_name">'+list_data[i].s_name+'</span>'+
                    '<p class="fr num">'+
                    '<span>'+list_data[i].l_viewing+'</span>'+
                    '<i></i>'+
                    '</p>'+
                    '</div>'+
                    '</div>';
                return back_html;
            }

            //点击加载更多获取列表数据  start
            function getListData(list_now,index){
                var param = {
                    url:list_now.interface_now,
                    data:{
                        page:list_now.page_now,
                        amount:list_now.amount
                    }
                };
                load_mark.eq(index).show();
                $.JsonRpc(param,function(data) {
                    //list_now.pushData(data.data);
                    var amount = data.content.amount;
                    data = data.data;
                    var tpl_now = "";
                    var list_num= data.num;
                    var list_data = data.list;
                    var nowPage = data.page;
                    if(Math.ceil(list_num/data.list.length)==nowPage){
                        list_now.btn_more.html("已经加载全部").addClass("disabled");
                    }else {
                        list_now.btn_more.html("加载更多");
                    }
                    $.each(list_data,function(i){
                        tpl_now += list_now.pushHtml(list_data,i,nowPage);
                    });
                    load_mark.eq(index).before(tpl_now);
                    if(index!=1){
                        listMove();
                    }else {
                        addStar();
                    }
                    load_mark.eq(index).hide();
                    if(data.page==Math.ceil(data.num/amount)){
                        list_now.btn_more.html("已经加载全部").addClass("disabled");
                    }
                });
            }
            var arr_home_init = [
                {
                    btn_more:live_more,
                    page_now:1,
                    amount:9,
                    interface_now:"Homes/liveList",
                    getData:getListData,
                    pushHtml:pushLiveHtml
                },
                {
                    btn_more:call_more,
                    page_now:1,
                    amount:6,
                    interface_now:"Homes/shoutList",
                    getData:getListData,
                    pushHtml:pushCallHtml

                },
                {
                    btn_more:back_more,
                    page_now:1,
                    amount:9,
                    interface_now:"Homes/playBackList",
                    getData:getListData,
                    pushHtml:pushBackHtml
                }
            ];
            $.each(arr_home_init,function(i){
                arr_home_init[i].btn_more.on("click",function(){
                    if(!$(this).hasClass("disabled")){
                        arr_home_init[i].page_now +=1;
                        getListData(arr_home_init[i],i);
                    }else{
                        return false;
                    }
                });
            });
            //点击加载更多获取列表数据  end
            getHomeData();
        });
    });
});